package cn.bjc.controller;

import cn.bjc.block.UserBlock;
import cn.bjc.domain.User;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
//动态刷新配置文件
@RefreshScope
public class UserController {

    @Value("${test.name}")
    private String name;
    
    @GetMapping("/getById/{id}")
    //要对哪个接口进行熔断,就在哪个接口加注解
    //- value：给当前接口取一个资源名称
    //- blockHandlerClass：指定降级方法类
    //- blockHandler：指定降级方法名称，blockHandler方法访问范围需要是 public，返回类型需要与接口相匹配，参数类型必须和接口一致并且最后加一个类型为 BlockException 的异常参数
    @SentinelResource(value="user-getById", blockHandler = "blockMethodsGetById", blockHandlerClass = UserBlock.class)
    public User getById(@PathVariable("id") Long id){
        return new User(id, "测试调用", "测试调用成功!" + name);
        
    }

    
    //限流方法1
    //public User blockMethodsGetById(@PathVariable("id") Long id, BlockException e){
    //    e.printStackTrace();
    //    return new User(-1L,"触发限流", "本类降级触发限流!" + name);
    //
    //}
        
}
